import { Component, ElementRef, EventEmitter, Injector, Input, OnInit, Output, TemplateRef } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { listAnimation } from '../../animations/list.animation';
import { NzMessageService } from 'ng-zorro-antd/message';
import { EventKeyEnum, EventPublishDTO, OptionTypeEnum, TableOptionModel } from './table-option/table-option.model';
import { ColumnType } from './enums/ColumnType';
import { iconData } from '../../common/IconData';
import { GlobalConfig } from '../../common/GlobalConfig';
import { TableColumnModel } from './model/tableColumn.model';

@Component({
    selector: 'app-table',
    templateUrl: './table.component.html',
    styleUrls: ['./table.component.scss'],
    animations: [listAnimation]
})
export class TableComponent implements OnInit{

    @Input()
    searchObj:any = {
        pageNum: 1,
        pageSize: 15
    };
    @Output()
    searchObjChange:EventEmitter<any> = new EventEmitter<any>();
    @Input()
    tableData: any[];
    @Input()
    tableKey = 'id';
    @Input()
    tableColumnArray: Array<TableColumnModel> = [];
    @Input()
    tableCaseMap: Map<any, Map<string, any>> = GlobalConfig.globalCaseMap;
    total: number;
    tableLoading = false;
    @Input()
    dataService: any;
    @Input()
    scrollValue = { x: '', y: '70vh' };
    @Input()
    optionModelList:TableOptionModel[] = null as TableOptionModel[];
    @Input()
    otherOptionModelList:TableOptionModel[] = null as TableOptionModel[];
    @Input()
    infoLink: string;
    @Input()
    topVisible = false;
    @Input()
    maxHeight = 40;
    @Input()
    subList: string[] = [];
    @Input()
    miniHeight = null;
    @Input()
    Search: TemplateRef<any>;
    @Input()
    optionWidth = '200px';
    @Input()
    hasExpand = false;

    constructor(
        private _injector: Injector,
        public _config: ConfigModule,
        private msg: NzMessageService,
        private el: ElementRef
    ) {
    }

    list(){
        this.tableLoading = true;
        this.dataService.list(this.searchObj).subscribe(
            (res) => {
                if (res.code===200){
                    this.tableData = res.rows;
                    this.total = res.total;
                    this.tableData.forEach(item => {
                        if (this.subList.includes(item[this.tableKey]+"")){
                            item.isCheckedButton=true;
                        }
                    })
                }
            }
        ).add( () => {
            this.tableLoading = false;
            this.setMiniTableHeight();
        });
    }

    ngOnInit(): void {
        this.list();
    }

    //设置表格高度
    setMiniTableHeight() {
        let height = window.innerHeight -300 ;
        if (this.miniHeight!= null){
            height = this.miniHeight;
        }
        this.el.nativeElement.querySelector('.ant-table-body').style.maxHeight = height + 'px';
        this.el.nativeElement.querySelector('.ant-table-body').style.height = height+ 'px'
    }


    @Output()
    optionEvent = new EventEmitter();

    protected readonly OptionTypeEnum = OptionTypeEnum;
    protected readonly OptionKeyEnum = EventKeyEnum;

    //触发事件
    eventPublish(item: any, eventKey: EventKeyEnum) {
        let data = {
            eventKey: eventKey,
            publishData: item
        } as EventPublishDTO;
        this.optionEvent.emit(data);
    }

    deleteItem(item: any) {
        this.dataService.delete(item[this.tableKey]).subscribe(
            (res) => {
                if (res.code===200){
                    this.list();
                }
            }
        );
    }

    //选中按钮
    checkedButtonChange(item:any) {
        if (item.isCheckedButton){
            let data = {
                eventKey: this.OptionKeyEnum.SELECT,
                publishData: item
            } as EventPublishDTO;
            this.optionEvent.emit(data);
        }else {
            let data = {
                eventKey: this.OptionKeyEnum.UNSELECT,
                publishData: item
            } as EventPublishDTO;
            this.optionEvent.emit(data);
        }
    }

    getItemValue(item: any, key: string) {
        if (key.includes(".")){
            let keys = key.indexOf(".");
            if (item[key.substring(0,keys)]==null){
                return "";
            }else {
                return this.getItemValue(item[key.substring(0,keys)], key.substring(keys+1,key.length))

            }
        }else {
            return item[key];
        }
    }

    protected readonly ColumnType = ColumnType;
    protected readonly iconData = iconData;
    protected readonly EventKeyEnum = EventKeyEnum;
}
